package vn.vnvas.xoso.mienbac.thongke;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.vnvas.xoso.R;
import vn.vnvas.xoso.utils.WebService_ThongKe;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class tab_thongke extends Activity {

	private ListView mListView;
	private ArrayList<MenuThongKe> mArrayList;
	private thongkeAdapter adapter;

	private WebService_ThongKe mWebService;
	private JSONArray jArray;
	private JSONObject jObject;

	private String jString, strMenu, strMenuId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_thongke);

		mListView = (ListView) findViewById(R.id.listViewThongKe);
		mArrayList = new ArrayList<MenuThongKe>();
		adapter = new thongkeAdapter(getBaseContext(), R.layout.layout_item,
				mArrayList);
		mListView.setAdapter(adapter);

		mWebService = new WebService_ThongKe();

		jString = mWebService.WsGETMENU("CreateMenuThongKe");

		try {
			jArray = new JSONArray(jString);
			if (jArray.length() > 0) {
				for (int i = 0; i < jArray.length(); i++) {
					jObject = jArray.getJSONObject(i);

					strMenu = jObject.getString("MenuValue");
					strMenuId = jObject.getString("MenuID");

					MenuThongKe menu = new MenuThongKe(strMenu,
							BitmapFactory.decodeResource(getResources(),
									R.drawable.ic_launcher), strMenuId);
					mArrayList.add(menu);

				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				String menuId = ((LayoutMenuItem)view).menu_id.getText().toString();
				Toast.makeText(getBaseContext(), "Da chon:"+menuId, Toast.LENGTH_LONG).show();
				Intent intent1 = new Intent(getBaseContext(), ThongKeChiTiet.class);
				intent1.putExtra("Id", menuId);
				startActivity(intent1);
				
			}
		});

	}

}
